package com.synnex.mobile.servier.core.survey.dao.impl;

import com.synnex.mobile.generic.dao.impl.GenericHibernateDaoImpl;
import com.synnex.mobile.servier.core.survey.dao.SurveyDao;
import com.synnex.mobile.servier.core.survey.dto.Survey;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 13-8-9
 * To change this template use File | Settings | File Templates.
 */
public class SurveyDaoImpl extends GenericHibernateDaoImpl<Survey,Integer> implements SurveyDao {

    public List<Survey> listSurvey(Integer qid) {
        Criteria c = this.getSession().createCriteria(Survey.class)

                .addOrder(Order.asc("seq"))
                .setFetchMode("answerItems", FetchMode.JOIN)
//                .createCriteria("answerItems").addOrder(Order.asc("seq"))
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        if(null != qid){
            c.add(Restrictions.eq("questionnaireId", qid));
        }

       List<Survey> surveys =  c.list();

        return surveys;
    }

    public void addSurvey(Survey dto) {
        super.save(dto);
    }

    public void editSurvey(Survey dto) {
       super.update(dto);
    }

    public void deleteSurveyById(Integer id){
        super.delete(super.get(id));
    }

    public Survey getSurveyById(Integer id) {
        return super.get(id);
    }

    public void deleteSurvey(Survey survey) {
        super.delete(survey);
    }
}
